import { emitter } from '@/utils/emitter.js'


export function convertToStructuredArray({
    dialogId,
    taskId,
    componentId,
    symbolImage = '',
    packageImage = '',
    pdfUrl = ''
}, file) {
    if (!dialogId || !taskId || !componentId) {
        throw new Error('Missing required fields');
    }

    const base = { dialogId, taskId, componentId };

    let mage = []

    if (!file) {
        mage.push({ role: "assistant", content: `您好！您需要提取 ${componentId} 型号的封装参数与原理图参数，进而生成对应的封装库文件。`, type: 'text' })
        mage.push({ role: "assistant", content: `首先我们需要先找到 ${componentId} 型号的PDF文件，然后进行解析，其原文如下：`, type: 'text' })
        mage.push({ ...base, type: "pdf", filetype: "pdf", pdfUrl, filename: getFilenameFromUrl(pdfUrl), state: 1, tabname: getFilenameFromUrl(pdfUrl) })
    }
    sendData({ ...base, type: "btu", filetype: "json", displayTexts: ['图片解析中...', '图片解析完成'], state: 0 })
    return [
        ...mage,
        // { role: "assistant", content: `解析`, type: 'text' },
        { ...base, type: "btu", filetype: "pdf", displayTexts: ['PDF定位中...', 'PDF定位完成'], state: 1, pdfUrl, tabname: getFilenameFromUrl(pdfUrl) },
        { ...base, type: "img1", symbolImage, packageImage },
        { ...base, type: "btu", filetype: "json", displayTexts: ['图片解析中...', '图片解析完成'], state: 0 }
    ];
}

export const sendData = (data) => {
    if (data.filetype == 'none') {

    } else if (data.filetype == 'img2' && data.state == 2) {

        emitter.emit('setRendering', data)

    } else {
        emitter.emit('update-data', data)
        emitter.emit('setRightStatus', true)
    }
}

export function processApiResponse({ dialogId, symbolImage, packageImage, taskId, componentId,
     symbolJsonName, symbolJsonContent, packageJsonName, packageJsonContent }) {
    return {
        dialogId, taskId, componentId,
        symbolImage, packageImage,
        symbolJsonName, packageJsonName,
        symbolJsonContent: JSON.parse(symbolJsonContent),
        packageJsonContent: JSON.parse(packageJsonContent),
        type: 'json'

    };
}
function getFilenameFromUrl(url) {
    return url.split('/').pop();
}

export function convertDigMages(data) {
    let dat1 = data.map(item => {
        return [...convertDigMageitem(item)]
    }).flat();
    console.log("uie1222", dat1);
    return dat1

}
/** 将数据拆解成n条 */
export function convertDigMageitem({
    taskId, dialogId, componentId, query, file, pdfUrl,
    symbolImage, packageImage, pdfFileName,
    symbolJsonName, symbolJsonContent, packageJsonName,
    packageJsonContent, symbolRendingImage,
    packageRendingImage, image3D, rendingFile3D
}) {
    if (!dialogId || !taskId || !componentId) {
        throw new Error('Missing required fields');
    }

    let runData = []

    const base = { dialogId, taskId, componentId };

    // 用于判断是不是第一条数据
    if (file !== null) {
        runData.push({ ...base, type: "pdf", pdfFileName, state: 1, pdfUrl, tabname: getFilenameFromUrl(pdfUrl), filetype: "pdf", filename: getFilenameFromUrl(pdfFileName), role: "user" })
    }

    // 如果用户发了文件并输入文字，则将文字填入下一条数据
    if (query !== null && query !== '') {
        runData.push({ ...base, type: "text", role: "user", content: query })
    }

    // 显示处理中
    runData.push({ role: "assistant", content: "已接受任务，开始处理...", type: 'text' })


    // 显示文件解析过程和结果
    if (!file) {
        runData.push({ role: "assistant", content: `您好！您需要提取 ${componentId} 型号的封装参数与原理图参数，进而生成对应的封装库文件。`, type: 'text' })
        runData.push({ role: "assistant", content: `首先我们需要先找到 ${componentId} 型号的PDF文件，然后进行解析，其原文如下：`, type: 'text' })
        runData.push({
            ...base, type: "pdf", filetype: "pdf", pdfFileName, state: 1,
            filename: getFilenameFromUrl(pdfFileName), pdfUrl, tabname: getFilenameFromUrl(pdfUrl)
        })
    }

    // runData.push({ role: "assistant", content: `解析`, type: 'text' })
    runData.push({ ...base, type: "btu", filetype: "pdf", displayTexts: ['PDF定位中...', 'PDF定位完成'], state: 1, pdfUrl, tabname: getFilenameFromUrl(pdfUrl) })
    runData.push({ ...base, type: "img1", symbolImage, packageImage })

    if (!symbolJsonName || !symbolJsonContent || !packageJsonName || !packageJsonContent) {
        return runData
    }

    if (packageJsonName && packageJsonContent) {
        runData.push({
            ...base, type: "btu", data: {
                symbolImage, packageImage,
                symbolJsonName, packageJsonName,
                symbolJsonContent: JSON.parse(symbolJsonContent),
                packageJsonContent: JSON.parse(packageJsonContent)
            }, filetype: "json", displayTexts: ['', '图片解析完成'], state: 1
        })
        runData.push({
            ...base, symbolJsonName, packageJsonName,
            symbolJsonContent: JSON.parse(symbolJsonContent),
            packageJsonContent: JSON.parse(packageJsonContent),
            type: 'json'
        })
    }

    if (image3D && rendingFile3D) {
        runData.push({
            ...base, type: "btu", filetype: "img2", tabname: componentId + '型号效果图', data: {
                symbolImage, packageImage, symbolRendingImage,
                packageRendingImage, image3D, rendingFile3D,
            }, displayTexts: ['', '效果图生成完成'], state: 1
        })
        runData.push({
            ...base, type: "img2", symbolImage, packageImage, symbolRendingImage,
            packageRendingImage, image3D, rendingFile3D
        })
        runData.push({
            ...base, type: "btu", filetype: "none", tabname: componentId + '型号效果图', data: {
                symbolImage, packageImage, symbolRendingImage,
                packageRendingImage, image3D, rendingFile3D,
            }, displayTexts: ['封装库生成中...', '封装库生成完成', '生成封装库'], state: 2
        })
    }

    return runData

}

